package com.rizhov.main;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PushbackInputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Client {

    /**
     * @param argsfdhfj
     */
    public static void main(String[] args) {
	Socket socket;
	try {
	    socket = new Socket(InetAddress.getByName(null), 4444);
	    ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(
		    socket.getOutputStream()));
	    oos.flush();
	    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
	    PushbackInputStream pis = null;
	    ClientList clientList = new ClientList(oos, ois, pis);

	    System.out.println("clientList.add(\"str #1\") is successful: "
		    + clientList.add("str #1"));
	    System.out.println("clientList.add(\"str #1\") is successful: "
		    + clientList.add("str #2"));
	    System.out.println("clientList.get(0) is " + clientList.get(0));
	    System.out.println("clientList.get(1) is " + clientList.get(1));
	    System.out.println("clientList.size() is " + clientList.size());
	    System.out.println("clientList.remove(\"str #1\") is successful: "
		    + clientList.remove("str #1"));
	    System.out.println("clientList.get(0) is " + clientList.get(0));
	    System.out.println("clientList.remove(\"str #2\") is successful: "
		    + clientList.remove("str #2"));
	    System.out.println("clientList.get(0) is " + clientList.get(0));
	    System.out.println("clientList.size() is " + clientList.size());

	    System.out.println("clientList.getAllTheList() contains: ");
	    List<String> tempList = clientList.getAllTheList();
	    if (tempList != null) {
		Iterator<String> it = tempList.iterator();
		while (it.hasNext()) {
		    System.out.println(it.next());
		}
	    } else {
		System.out.println("null");
	    }

	    System.out.println("clientList.add(\"str #1\") is successful: "
		    + clientList.add("str #1"));
	    System.out.println("clientList.add(\"str #1\") is successful: "
		    + clientList.add("str #2"));

	    System.out.println("clientList.getAllTheList() contains: ");
	    tempList = clientList.getAllTheList();
	    if (tempList != null) {
		Iterator<String> it = tempList.iterator();
		while (it.hasNext()) {
		    System.out.println(it.next());
		}
	    } else {
		System.out.println("null");
	    }

	    Thread tr = new Thread() {
		@Override
		public void run() {
		    try {
			sleep(10000);
		    } catch (InterruptedException e) {
			e.printStackTrace();
		    }
		};
	    };
	    tr.start();
	    tr.join();
	    ArrayList<Object> closeCommandList = new ArrayList<Object>();
	    closeCommandList.add(new Integer(CommandDescriptor.CLOSE_THREAD.getCode()));
	    oos.writeObject(closeCommandList);
	    oos.close();
	    ois.close();
	} catch (UnknownHostException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
    }
}
